<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            position: absolute;
            left: 120px;
            top: 150px;
            width: 100px;
            height: 100px;
            background-color: red;

        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        var oBox = document.querySelector('#box');
        document.onkeyup = function (e) {
            console.log('move');
            e = e || window.event;
            // 1. 获取按的是上下左右哪一个键？keyCode
            // if(e.keyCode === 37){
            //     //向左移动
            // }else if(e.keyCode === 38){

            // }
            var speed = 5;
            switch (e.keyCode) {
                case 37:// 向左移动

                    break;
                case 38:// 向上移动

                    break;
                case 39:// 向右移动
                    // 1. 获取原来的left值
                    var oldLeft = oBox.offsetLeft;
                    console.log(oldLeft);
                    // 2. 增加一定的数量
                    var newLeft = oldLeft + speed;
                    // 3. 把新的left值，赋值给oBox的left属性
                    oBox.style.left = newLeft + 'px';
                    break;
                case 40:// 向下移动

                    break;
            }

        }
    </script>
</body>

</html>